package com.aiiju.core.service;

import java.io.File;
import java.io.InputStream;

import org.apache.commons.lang.StringUtils;

import com.aiiju.core.service.tool.IoService;
import com.aiiju.core.service.tool.ReadService;
import com.aiiju.core.util.DeleteFileUtil;
import com.aiiju.core.util.FileCodeUtils;
import com.aiiju.core.util.OpenOffice2PDF;
import com.aiiju.core.util.SysUtil;
import com.aiiju.core.util.UuidUtils;
import com.tongdainfo.common.parser.HtmlParser;
import com.tongdainfo.common.parser.IParser;
import com.tongdainfo.common.parser.PdfParser;
import com.tongdainfo.common.parser.TxtParser;
import com.tongdainfo.common.parser.Word2007Parser;
import com.tongdainfo.common.parser.Word98_2003Parser;

public class WordService {
	
	
	public  String getInputString(InputStream input, String ext) throws Exception {
		String result = null;
		IParser  parse = null;
		switch (ext) {
		/*"doc", "docx", "xls",
        "xlsx", "ppt", "pptx"*/
		case "doc":;
		case "docx":;
		case "xls":;
		case "xlsx":;
		case "ppt":;
		case "pptx":result = this.openOfficeToString(input, ext);
			break;
		case "pdf":
		case "txt": 
		case "html":result = this.readfileContext(input, ext);
			break;	
		default:
			break;
		}
		
		// 回车换行，进行重新判断
		if(StringUtils.isNotBlank(result)){		
			result = ReadService.toNewString(result);
		}
		
		return result;
	}
	/**
	 * 通过openoffice 读取文本的内容
	 * @Description:
	 * @param input
	 * @param ext
	 * @return
	 * @throws Exception
	 * @author: aiju
	 * @time:2017年12月1日 上午10:08:33
	 */
	public String openOfficeToString(InputStream input, String ext) throws Exception {
		/**
		 * 接收文件
		 * 转化文件
		 */
		// 接收后存储到本地
		String orgFile = IoService.getFile(input, ext);
		// 文件格式转成txt  缓存中的txt文件
		String uuidStr = UuidUtils.uuidStr();
		String sysCachFile = SysUtil.getSysCachFile();
		String txtFile = sysCachFile.concat(uuidStr).concat(".").concat("txt");
		
		new OpenOffice2PDF().openOffice2Pdf(orgFile, txtFile);
		
		/**
		 * 读取txt文件
		 */
		String fileContent = ReadService.readFile(txtFile);
		
		/**
		 * 删除缓存文件
		 */
		DeleteFileUtil.deleteFile(txtFile);
		DeleteFileUtil.deleteFile(orgFile);
		
		return fileContent;
	}
	
	public String readfileContext(InputStream input, String extensionName) throws Exception {
		
		/**
		 * 类型的判断
		 */
		IParser  parse = null;
//		String extensionName = FileTool.getExtensionName(filename);
		switch (extensionName) {
		case "doc":
			parse = new Word98_2003Parser();
			break;
		case "docx":
			parse = new Word2007Parser();
			break;
		case "pdf":
			parse = new PdfParser();
			break;
		case "txt":
			parse = new TxtParser();
		case "html":
			parse = new HtmlParser();
			break;
		default:
		}
		
//		String result = parse.readText(file, "GB2312");
		String result = null;
		/**
		 * 存储到本地
		 */
		String fileName = IoService.getFile(input, extensionName);
		
		File file = new File(fileName);
		String codeString = FileCodeUtils.codeString(fileName);
		result = parse.readText(file, codeString);
		
		/**
		 * 删除文件
		 */
		DeleteFileUtil.deleteFile(fileName);
		
		return result;
	}

}
